package ru.galiauir.utils;

import java.util.Random;

public class RandomUtils {
	
	public static final char[] alphabet = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	
//	public static RandomEngine engine = new DRand();
//	public static Poisson poisson;
//	
//	public static RandomEngine engine1 = new DRand();
//	public static Poisson poisson1;
	
	//public static double mju;
	
	private static Random rnd = new Random();
	
	public static double calculateMju(float alpha, long num) {
		// return mju = -Math.log(Math.pow((1 - alpha), num));
		return -Math.log(Math.pow((1 - alpha), num));
	}
	
//	public static int nextPoissonForNumberOfWorkers(double mju) {
//		if (poisson == null) {
//			poisson = new Poisson(mju, engine);
//		}
//		return poisson.nextInt();
//	}
//	
//	public static int nextPoissonForNumberOfInvetees(double mju) {
//		if (poisson1 == null) {
//			poisson1 = new Poisson(mju, engine1);
//		}
//		return poisson.nextInt();
//	}
	
	public static int nextPoisson(double mju) {
		  double L = Math.exp(-mju);
		  double p = 1.0;
		  int k = 0;
		  //Random rnd = new Random(System.currentTimeMillis());
		  do {
		    k++;
		    p *= rnd.nextDouble();
		  } while (p > L);

		  return k - 1;			
	}
	
	public static long nextNormal(long base) {
		//Random rnd = new Random();
		return (long) (base * Math.abs(rnd.nextGaussian()));
	}
	
	public static long nextNormal(long average, long disp) {
		//Random rnd = new Random();
		return (long) (Math.abs(average + disp * rnd.nextGaussian()));
	}
	
	public static String nextSessionId() {
		StringBuffer sb = new StringBuffer();
		//Random rnd = new Random();
		for (int i = 0; i < 10; i ++) {
			sb.append(alphabet[rnd.nextInt(alphabet.length)]);
		}
		return sb.toString();
	}
	
	public static int nextUniform(int max) {
		return rnd.nextInt(max);
	}
	
//	public static void cleanPoisson() {
//		poisson = null;
//	}
	
}
